{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# series",
   "id": "aa1b3954a2cca411"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## series的创建",
   "id": "b6ddbd8cf614defd"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "s = pd.Series([1,3,2,4,5])\n",
    "print(s)#左边为索引,右边为值\n",
    "#索引值可自定义\n",
    "s1 = pd.Series([1,2,3,4],index=['a','b','c','d'],name = 'test')\n",
    "print(s1)"
   ],
   "id": "310cae07007549e5",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#可以通过字典创建\n",
    "s = pd.Series({'a':1,'b':2,'c':3,'d':4})\n",
    "print(s)\n",
    "s1 = pd.Series(s,index = ['a','d'])\n",
    "print(s1)"
   ],
   "id": "21539188ffd96096",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## series的属性\n",
    "index value dtype shape ndim size name\n",
    "loc[]:标签索引\n",
    "iloc[]:位置索引\n",
    "at[]:标签访问\n",
    "iat[]:位置访问"
   ],
   "id": "f052a83cd4025271"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "print(s.index)\n",
    "print(s.values)\n",
    "print(s.shape,s.ndim,s.size)\n",
    "print(s.dtype,s.name)"
   ],
   "id": "10c8dca5b5252cd",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "print(s.loc['b'])\n",
    "print(s.iloc[1])"
   ],
   "id": "92e0b60b8bd5b717",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import  pandas as pd\n",
    "s = pd.Series([1,3,2,4,5])\n",
    "print(s[0])"
   ],
   "id": "5814d5ff0dc13de2",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 支持布尔索引\n",
    "print(s[s>=2])"
   ],
   "id": "7f24fca564f3f1b7",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## series方法",
   "id": "3e923e32a28184b9"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "head() tail() isin() isna() mode() quantile desscribe() value_counts() count()\n",
    "nunique() drop_duplicates() sample() sort_index() sort_values() replace() keys()"
   ],
   "id": "3c70252361462125"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "同numpy:sum() mean() min() max() var() std() median() unique()",
   "id": "e84b21a058ec99ef"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "s2 = pd.Series([1,3,2,4,5,6,7,8,9,10])\n",
    "print(s2.head())#取前n行,没有参数默认前五行\n",
    "print(s2.tail())#取后n行,没有参数默认后五行"
   ],
   "id": "9f563a23294e6f1a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "s = pd.Series([10,1,np.nan,None,7,8,9,1,1,7],name = 'test')\n",
    "print(s)"
   ],
   "id": "9e4b9ca825551617",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "s.head(3)",
   "id": "7d2a8484fcac4a9a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "s.tail(3)",
   "id": "66482c36e654e927",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 查看所有的描述性信息\n",
    "s.describe()"
   ],
   "id": "1d27b42c0bfd88ba",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 获取元素个数(忽略缺失值)\n",
    "print(s.count())"
   ],
   "id": "81403b4853ee072d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#获取索引\n",
    "print(s.keys())\n",
    "print(s.index)#index是属性"
   ],
   "id": "a939d79c8ca0cca5",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#是否是缺失值\n",
    "s.isna()"
   ],
   "id": "be09ef2e275b72ab",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "s.isin([np.nan,1,9])",
   "id": "14406a897fdd19dd",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#常用计算方法\n",
    "print(s.mean(),s.sum(),s.std(),s.var(),s.min(),s.max())"
   ],
   "id": "f37fc608f4ec5d3c",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 排序\n",
    "s.sort_values()#按值排序"
   ],
   "id": "42b3ef57caf62a76",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "s.sort_index()#按索引值排序",
   "id": "9358e60321bb9874",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 分位数\n",
    "print(s.quantile(0.25))"
   ],
   "id": "17d1bf28f1145c45",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 众数\n",
    "print(s.mode())#出现次数多的会排在上面"
   ],
   "id": "8c6043dc8ca133aa",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "#去重\n",
    "s.drop_duplicates()\n",
    "#s.unique()#返回类型与drop_duplicates()的不同\n",
    "print(s.unique())#去重后的元素个数"
   ],
   "id": "3ded98c7f265a142",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# dataframe",
   "id": "d24855c328ec34a3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T01:35:38.682978Z",
     "start_time": "2025-07-29T01:35:36.044039Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {'id':[1,2,3],\n",
    "     'name':['tom','jack','bob'],\n",
    "     'age':[4,5,6],\n",
    "     'score':[1,2,50]\n",
    "     },index=['a','b','c']#,columns=['id','score']\n",
    ")\n",
    "df"
   ],
   "id": "e038307d9c15bef8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   id  name  age  score\n",
       "a   1   tom    4      1\n",
       "b   2  jack    5      2\n",
       "c   3   bob    6     50"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "      <td>tom</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>2</td>\n",
       "      <td>jack</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>3</td>\n",
       "      <td>bob</td>\n",
       "      <td>6</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T01:39:27.539768Z",
     "start_time": "2025-07-29T01:39:27.534134Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(type(df['id']))\n",
    "print(type(df))"
   ],
   "id": "665b3bfe9141008b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.series.Series'>\n",
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## dataframe 属性",
   "id": "17f3a1f687850752"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "index values dtypes shape ndim size columns loc[] iloc[] at[] iat[] T",
   "id": "9fbe9b39fdf5dbb4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T01:44:58.147888Z",
     "start_time": "2025-07-29T01:44:58.137270Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#行索引\n",
    "print(df.index)\n",
    "#列标签\n",
    "print(df.columns)\n",
    "#值\n",
    "print(df.values)"
   ],
   "id": "dd6929910d91ec89",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['a', 'b', 'c'], dtype='object')\n",
      "Index(['id', 'name', 'age', 'score'], dtype='object')\n",
      "[[1 'tom' 4 1]\n",
      " [2 'jack' 5 2]\n",
      " [3 'bob' 6 50]]\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T01:47:36.245164Z",
     "start_time": "2025-07-29T01:47:36.239617Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#维度\n",
    "print(df.ndim)\n",
    "#数据类型\n",
    "print(df.dtypes)\n",
    "#形状\n",
    "print(df.shape)\n",
    "#元素个数\n",
    "print(df.size)"
   ],
   "id": "60a4e759d49e0d2e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "id        int64\n",
      "name     object\n",
      "age       int64\n",
      "score     int64\n",
      "dtype: object\n",
      "(3, 4)\n",
      "12\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T01:47:59.390634Z",
     "start_time": "2025-07-29T01:47:59.383489Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#转置\n",
    "print(df.T)"
   ],
   "id": "fc8f69c25206200c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         a     b    c\n",
      "id       1     2    3\n",
      "name   tom  jack  bob\n",
      "age      4     5    6\n",
      "score    1     2   50\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T02:11:11.314162Z",
     "start_time": "2025-07-29T02:11:11.309915Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#索引\n",
    "#获取一列元素\n",
    "print(df.loc[:,'name'])\n",
    "print(df.iloc[:,1])\n",
    "#获取单个元素\n",
    "print(df.loc['b','name'])\n",
    "print(df.iloc[1,1])"
   ],
   "id": "6515b0d8cea63463",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a     tom\n",
      "b    jack\n",
      "c     bob\n",
      "Name: name, dtype: object\n",
      "a     tom\n",
      "b    jack\n",
      "c     bob\n",
      "Name: name, dtype: object\n",
      "jack\n",
      "jack\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T04:01:46.920727Z",
     "start_time": "2025-07-29T04:01:46.903140Z"
    }
   },
   "cell_type": "code",
   "source": "df[['name','id']]",
   "id": "bdf36001880191b5",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   name  id\n",
       "a   tom   1\n",
       "b  jack   2\n",
       "c   bob   3"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>tom</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>jack</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>bob</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## dataframe方法",
   "id": "a764fb59ab07a5c1"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "value_counts():每个唯一值的出现次数 count():非缺失值的数量 duplicated()是否重复 sample()随机抽样 replace()替换值 sort_index()按索引排序 sort_values()安置排序 nlargest()返回某列最大的n条数据 nsmallest()返回某列最小的n条数据",
   "id": "861db8e5016f584b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T05:52:26.935741Z",
     "start_time": "2025-07-29T05:52:26.928745Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#查看前n行数据,默认5行\n",
    "print(df.head(2))\n",
    "#查看后n行的数据,默认5行\n",
    "print(df.tail(2))"
   ],
   "id": "83e6a07eb416d652",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   id  name  age  score\n",
      "a   1   tom    4      1\n",
      "b   2  jack    5      2\n",
      "   id  name  age  score\n",
      "b   2  jack    5      2\n",
      "c   3   bob    6     50\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T05:54:20.604882Z",
     "start_time": "2025-07-29T05:54:20.571163Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#查看元素是否存在于参数集合中\n",
    "df.isin(['bob'])"
   ],
   "id": "10758f8fe529f8c7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      id   name    age  score\n",
       "a  False  False  False  False\n",
       "b  False  False  False  False\n",
       "c  False   True  False  False"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T05:55:48.699795Z",
     "start_time": "2025-07-29T05:55:48.690284Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#查看元素是否有缺失值\n",
    "df.isna()"
   ],
   "id": "9239e76633ed6f15",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      id   name    age  score\n",
       "a  False  False  False  False\n",
       "b  False  False  False  False\n",
       "c  False  False  False  False"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T06:02:13.863359Z",
     "start_time": "2025-07-29T06:02:13.813412Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#求和\n",
    "print(df['score'].sum())\n",
    "#最值\n",
    "print(df['score'].max())\n",
    "print(df['score'].min())\n",
    "#平均数\n",
    "print(df['score'].mean())\n",
    "#中位数\n",
    "print(df['score'].median())\n",
    "#众数\n",
    "print(df['score'].mode())"
   ],
   "id": "f692f84407543a8e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "53\n",
      "50\n",
      "1\n",
      "17.666666666666668\n",
      "2.0\n",
      "0     1\n",
      "1     2\n",
      "2    50\n",
      "Name: score, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T06:19:50.688634Z",
     "start_time": "2025-07-29T06:19:50.657475Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#查看描述性信息\n",
    "df.describe()"
   ],
   "id": "8a0d5ba181daa2e6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "        id  age      score\n",
       "count  3.0  3.0   3.000000\n",
       "mean   2.0  5.0  17.666667\n",
       "std    1.0  1.0  28.005952\n",
       "min    1.0  4.0   1.000000\n",
       "25%    1.5  4.5   1.500000\n",
       "50%    2.0  5.0   2.000000\n",
       "75%    2.5  5.5  26.000000\n",
       "max    3.0  6.0  50.000000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>age</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>17.666667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>28.005952</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.5</td>\n",
       "      <td>4.5</td>\n",
       "      <td>1.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.5</td>\n",
       "      <td>5.5</td>\n",
       "      <td>26.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>3.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>50.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T06:23:18.197522Z",
     "start_time": "2025-07-29T06:23:18.166825Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#每一列非缺失值的个数\n",
    "print(df.count())\n",
    "#出现的次数\n",
    "print(df.value_counts())"
   ],
   "id": "ad2e2826af587e93",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id       3\n",
      "name     3\n",
      "age      3\n",
      "score    3\n",
      "dtype: int64\n",
      "id  name  age  score\n",
      "1   tom   4    1        1\n",
      "2   jack  5    2        1\n",
      "3   bob   6    50       1\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T06:26:15.408881Z",
     "start_time": "2025-07-29T06:26:15.400882Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#检查是否重复\n",
    "df.duplicated(subset = ['name'])"
   ],
   "id": "6d10de40472f30d1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    False\n",
       "b    False\n",
       "c    False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T06:27:57.679538Z",
     "start_time": "2025-07-29T06:27:57.669212Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#替换\n",
    "df.replace('bob','pop')"
   ],
   "id": "a422f28f4d80e9d2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   id  name  age  score\n",
       "a   1   tom    4      1\n",
       "b   2  jack    5      2\n",
       "c   3   pop    6     50"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "      <td>tom</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>2</td>\n",
       "      <td>jack</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>3</td>\n",
       "      <td>pop</td>\n",
       "      <td>6</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T06:28:37.685416Z",
     "start_time": "2025-07-29T06:28:37.672468Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#取样\n",
    "df.sample(1)"
   ],
   "id": "7584ff1a36c93fcf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   id name  age  score\n",
       "a   1  tom    4      1"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "      <td>tom</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T06:42:31.278294Z",
     "start_time": "2025-07-29T06:42:31.272884Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#排序\n",
    "print(df.sort_values(by = ['score','age'],ascending = [True,False]))"
   ],
   "id": "33556655c3bc40ae",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   id  name  age  score\n",
      "a   1   tom    4      1\n",
      "b   2  jack    5      2\n",
      "c   3   bob    6     50\n"
     ]
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T06:44:36.243610Z",
     "start_time": "2025-07-29T06:44:36.228235Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#获取前n行最大的数据\n",
    "df.nlargest(2,columns = ['score','age'])"
   ],
   "id": "5fbdf65bd2822593",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   id  name  age  score\n",
       "c   3   bob    6     50\n",
       "b   2  jack    5      2"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>3</td>\n",
       "      <td>bob</td>\n",
       "      <td>6</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>2</td>\n",
       "      <td>jack</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-29T07:59:12.160845Z",
     "start_time": "2025-07-29T07:59:12.151269Z"
    }
   },
   "cell_type": "code",
   "source": "df[['name','age']]",
   "id": "7014f2d0eeb11a9d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   name  age\n",
       "a   tom    4\n",
       "b  jack    5\n",
       "c   bob    6"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>tom</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>jack</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>bob</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "9f36bd203afbd6bf"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
